home *** CD-ROM | disk | FTP | other *** search
- /*
- * Routines to compress and uncompress tcp packets (for transmission
- * over low speed serial lines.
- *
- * Copyright (c) 1989 Regents of the University of California.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley. The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
- *
- * Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989:
- * - Initial distribution.
- *
- * modified for KA9Q Internet Software Package by
- * Katie Stevens (dkstevens@ucdavis.edu)
- * University of California, Davis
- * Computing Services
- * - 01-31-90 initial adaptation (from 1.19)
- * PPP.05 02-15-90 [ks]
- * PPP.08 05-02-90 [ks] use PPP protocol field to signal compression
- * PPP.15 09-90 [ks] improve mbuf handling
- * PPP.16 11-02 [karn] substantially rewritten to use NOS facilities
- *
- * - Feb 1991 Bill_Simpson@um.cc.umich.edu
- * variable number of conversation slots
- * allow zero or one slots
- * separate routines
- * status display
- *
- * 1992-93 Modifications for MacPPP.
- * -Larry Blunk, Merit Network, Inc./ University of Michigan
- */
-
- #include "ppp.h"
- #include "slhc.h"
-
- /* Initialize compression data structure
- * slots must be in range 0 to 255 (zero meaning no compression)
- */
- void
- slhc_init( LapInfo *lap, short rslots, short tslots )
- {
- b_16 i;
- struct cstate *ts;
-
- if ( rslots > 0 && rslots <= MAXSLOTS ) {
- lap->comp.rstate = lap->rcvslots;
- lap->comp.rslot_limit = rslots - 1;
- }
-
- if ( tslots > 0 && tslots <= MAXSLOTS ) {
- lap->comp.tstate = lap->txslots;
- lap->comp.tslot_limit = tslots - 1;
- }
-
- lap->comp.xmit_oldest = 0;
- lap->comp.xmit_current = lap->comp.recv_current = 255;
-
- if ( tslots > 0 ) {
- ts = lap->comp.tstate;
- for(i = lap->comp.tslot_limit; i > 0; --i){
- ts[i].this = i;
- ts[i].next = &(ts[i - 1]);
- }
- ts[0].next = &(ts[lap->comp.tslot_limit]);
- ts[0].this = 0;
- }
- }
-
- /* Encode a number */
- static b_8 *
- encode(b_8 *cp, b_16 n)
- {
- if(n >= 256 || n == 0){
- *cp++ = 0;
- *cp++ = n >> 8;
- }
- *cp++ = n;
- return cp;
- }
-
- /* Decode a number */
- static long
- decode(struct bufheader *bufptr)
- {
- short x;
-
- x = yankbyte(bufptr);
- if (x == 0) {
- return yank16(bufptr); /* yank16 returns -1 on error */
- } else {
- return (long)x; /* -1 if error */
- }
- }
-
- short
- slhc_compress(LapInfo *lap, struct bufheader *bufptr, short compress_cid)
- {
- struct slcompress *comp = &(lap->comp);
- struct cstate *ocs = &(comp->tstate[comp->xmit_oldest]);
- struct cstate *lcs = ocs;
- struct cstate *cs = lcs->next;
- b_16 hiplen, htcplen, hlen;
- b_16 ipoptlen, tcpoptlen;
- struct tcpheader *oth;
- unsigned long deltaS, deltaA;
- b_16 changes = 0;
- b_8 new_seq[16];
- b_8 *cp = new_seq;
- struct tcpheader th;
- struct ipheader iph;
-
- /* Extract IP header */
- hiplen = getipheader(&iph,bufptr);
- /* Bail if this packet isn't TCP, or is an IP fragment */
- if(iph.protocol != TCP_PROTOCOL || (iph.offset & 0x3fff) != 0 ){
- bufptr->dataptr -= hiplen;
- bufptr->length += hiplen;
- return SL_TYPE_IP;
- }
- /* Extract TCP header */
- htcplen = gettcpheader(&th,bufptr);
- hlen = hiplen + htcplen;
-
- /* Bail if the TCP packet isn't `compressible' (i.e., ACK isn't set or
- * some other control bit is set).
- */
- if((th.flags & ( TCP_FIN | TCP_SYN | TCP_ACK | TCP_RST )) ^ TCP_ACK ){
- /* TCP connection stuff; send as regular IP */
- bufptr->dataptr -= hlen;
- bufptr->length += hlen;
- return SL_TYPE_IP;
- }
- /*
- * Packet is compressible -- we're going to send either a
- * COMPRESSED_TCP or UNCOMPRESSED_TCP packet. Either way,
- * we need to locate (or create) the connection state.
- *
- * States are kept in a circularly linked list with
- * xmit_oldest pointing to the end of the list. The
- * list is kept in lru order by moving a state to the
- * head of the list whenever it is referenced. Since
- * the list is short and, empirically, the connection
- * we want is almost always near the front, we locate
- * states via linear search. If we don't find a state
- * for the datagram, the oldest state is (re-)used.
- */
- for ( ; ; ) {
- if( iph.source_addr == cs->cs_ip.source_addr
- && iph.dest_addr == cs->cs_ip.dest_addr
- && th.source_port == cs->cs_tcp.source_port
- && th.dest_port == cs->cs_tcp.dest_port)
- goto found;
-
- /* if current equal oldest, at end of list */
- if ( cs == ocs )
- break;
- lcs = cs;
- cs = cs->next;
- };
- /*
- * Didn't find it -- re-use oldest cstate. Send an
- * uncompressed packet that tells the other side what
- * connection number we're using for this conversation.
- *
- * Note that since the state list is ci